package com.engine.jucailinkq.attendance.attendanceanalysis.thread;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.UtilService;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.impl.UtilServiceImpl;
import com.engine.jucailinkq.attendance.attendanceanalysis.wrapper.AttendanceAnalysisWrapper;
import com.engine.jucailinkq.attendance.attendanceplan.service.AttendancePlanService;
import com.engine.jucailinkq.attendance.attendanceplan.service.impl.AttendancePlanServiceImpl;
import com.engine.jucailinkq.attendance.workflow.service.AllowanceService;
import com.engine.jucailinkq.attendance.workflow.service.impl.AllowanceServiceImpl;
import com.engine.jucailinkq.common.util.DateUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/10/9
 * @Description
 * @Author AdminZm
 */
public class AttendanceAnalysisSingleThread extends BaseBean implements Runnable{

    private String userId;
    private String startBeforeDate;
    private String endAfterDate;
    private String startDate;

    private int betweenDays;

    private Map<String,List<Map<String,Object>>> userGroupMap;

    private Map<String,Object> departEmployeeMap;

    private Map<String, List<Map<String,Object>>> collect;

    private Map<String,List<Map<String,Object>>> attendaceGroupById;

    private AttendancePlanService basicsetService = ServiceUtil.getService(AttendancePlanServiceImpl.class);

    private UtilService utilService = ServiceUtil.getService(UtilServiceImpl.class);

    private AttendanceAnalysisWrapper attendanceAnalysisService = ServiceUtil.getService(AttendanceAnalysisWrapper.class);

    private AllowanceService allowanceService = ServiceUtil.getService(AllowanceServiceImpl.class);

    public AttendanceAnalysisSingleThread(String userId, Map<String,Object> departEmployeeMap, String startBeforeDate,
                                          String endAfterDate, Map<String, List<Map<String,Object>>> collect, Map<String,List<Map<String,Object>>> attendaceGroupById,
                                          int betweenDays, String startDate, Map<String,List<Map<String,Object>>> userGroupMap) {
        this.userId = userId;
        this.departEmployeeMap = departEmployeeMap;
        this.startBeforeDate = startBeforeDate;
        this.endAfterDate = endAfterDate;
        this.collect = collect;
        this.attendaceGroupById = attendaceGroupById;
        this.betweenDays = betweenDays;
        this.startDate = startDate;
        this.userGroupMap = userGroupMap;
    }

    @Override
    public void run() {
        writeLog("AttendanceAnalysisSingleThread start:" + userId);
        try {
            /**获得人员考勤项目*/
            Map<String, Object> param = Maps.newHashMap();
            //入职日期
            String companystartDate = Util.null2String(userGroupMap.get(userId).get(0).get("companystartdate"));
            //离职日期
            String terminationDate = Util.null2String(departEmployeeMap.get(userId));

            //出勤结果，进出时间
            Map<String, Map<String, Object>> clockInTimeMap = Maps.newHashMap();
            param.put("startDate", startBeforeDate);
            param.put("endDate", endAfterDate);
            param.put("resourceId", userId);
            Map<String, Object> dataMap = basicsetService.getAttendanceItemsByPerson(param);
            List<Map<String, Object>> attendanceItems = (List<Map<String, Object>>) dataMap.get("data");

            /**
             *获得人员综合工时
             */
            Map<String, Object> workHourdataMap = basicsetService.getWorkHoursItemByPerson(param);
            List<Map<String, Object>> workHourItems = (List<Map<String, Object>>) workHourdataMap.get("data");


            /** 获取人员班次*/
            Map<String, Object> classesParamMap = Maps.newHashMap();
            classesParamMap.put("tableName", "uf_pbjg");
            classesParamMap.put("startDate", startBeforeDate);
            classesParamMap.put("endDate", endAfterDate);
            classesParamMap.put("pblx", "0");
            classesParamMap.put("pbdx", userId);
            classesParamMap.put("current", "1");
            classesParamMap.put("pageSize", 9999);
            classesParamMap.put("recurrence", 1);
            classesParamMap.put("clockInTimeMap", clockInTimeMap);
            classesParamMap.put("clockInData", collect.get(userId));
            classesParamMap.put("attendanceItems", attendanceItems);
            writeLog("AttendanceAnalysisSingleThread classesParamMap:" + JSONObject.toJSONString(classesParamMap));
            Map<String, Object> schedulMap = utilService.getSchedulingInFormation(classesParamMap);
            writeLog("AttendanceAnalysisSingleThread schedulMap:" + JSONObject.toJSONString(schedulMap));

            Map<String, List<Map<String, Object>>> attendaceResultMap = Maps.newHashMap();
            if (attendaceGroupById.get(userId) != null && attendaceGroupById.get(userId).size() > 0) {
                attendaceResultMap = attendaceGroupById.get(userId).stream().collect(Collectors.groupingBy(e -> e.get("rq").toString()));
            }
            writeLog("AttendanceAnalysisSingleThread attendaceResultMap:" + JSONObject.toJSONString(attendaceResultMap));
            for (int i = 0; i <= betweenDays; i++) {
                String date = DateUtil.AfterDay(startDate, i);
                if (("".equals(companystartDate) || DateUtil.getTime(date).compareTo(DateUtil.getTime(companystartDate)) > 0)
                        && ("".equals(terminationDate) || DateUtil.getTime(date).compareTo(DateUtil.getTime(terminationDate)) < 0)) {
                    try {
                        attendanceAnalysisService.attendanceAnalysisForApi(userId, date, collect.get(userId), attendanceItems, schedulMap, workHourItems, clockInTimeMap, attendaceResultMap.get(date));
                    } catch (Exception e) {
                        writeLog("AttendanceAnalysisSingleThread B param:" + userId + "、" + date + "; error:" + e.getMessage());
                    }
                }

                // 入职日期及之前、离职日期及之后的数据逻辑删除
                if ((StringUtils.isNotEmpty(companystartDate) && DateUtil.getTime(date).compareTo(DateUtil.getTime(companystartDate)) <= 0)
                        || (StringUtils.isNotEmpty(terminationDate) && DateUtil.getTime(date).compareTo(DateUtil.getTime(terminationDate)) >= 0)) {
                }
            }
        } catch (Exception e) {
            writeLog("AttendanceAnalysisSingleThread error:" + e);
        }
    }
}
